package cn.edu.ctbu.demo2.webapi;
import cn.edu.ctbu.demo2.core.PageUtils;
import cn.edu.ctbu.demo2.domain.Classes;
import cn.edu.ctbu.demo2.service.ClassesService;
import cn.edu.ctbu.demo2.utils.RUtil;
import cn.edu.ctbu.demo2.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import java.util.List;

@RestController
@RequestMapping("/webapi/classes")
@Slf4j
public class ClassesRestController {

    @Qualifier("classesimplService")
    @Autowired
    private ClassesService studentService;

    //    读取的ur1:/webapi/student/list,通过此json串读取需要的内容
    @GetMapping("/list")
    public List<Classes> getAll(){

        List<Classes> students=studentService.findAll();

        return  students;

    }

    /**
     * 统一返回值
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/list1")
    public R<Page<Classes>> list(int pageIndex, int pageSize){
        Pageable pageable=PageRequest.of(pageIndex,pageSize,Sort.by("id").descending());
        return RUtil.success(studentService.findAll(pageable));
    }


    //    分页，读取全部内容
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name){
//        按id排倒序
        Sort sort= Sort.by(Sort.Direction.DESC, "id");

        Page<Classes> studentPage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage=studentService.findAll(pageable);
        }
        else{
            Classes student=new Classes();
            student.setName(name);

            Pageable pageable = PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Classes> example=Example.of(student,matcher);


            studentPage=studentService.finfAll(example,pageable);

        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;

    }

    //根据学号分页
    @GetMapping("/getbypageno")
    public PageUtils getByPageNo(@RequestParam(value = "page",defaultValue = "0") Integer page,
                                 @RequestParam(value = "size",defaultValue = "10") Integer size,
                                 @RequestParam(value = "no",defaultValue = "")String no,@RequestParam(value = "name",defaultValue = "")String name){
//        按id排倒序
        Sort sort= Sort.by(Sort.Direction.DESC, "id");

        Page<Classes> studentPage;
        if(StringUtils.isEmpty(no)){
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage=studentService.findAll(pageable);
        }
        else{
            Classes student=new Classes();
            student.setNo(no);

            Pageable pageable = PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Classes> example=Example.of(student,matcher);


            studentPage=studentService.finfAll(example,pageable);

        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;
    }



    //    读取的ur1:/webapi/student/getid,通过此json串读取需要的内容
    @GetMapping("/get/{id}")
    public Classes get(@PathVariable Long id){

        Classes student=studentService.getById(id);

        return student;

    }


    //    读取的ur1:/webapi/student/getsname,通过此json串读取需要的内容
    @GetMapping("/gets/{name}")
    public Classes gets(@PathVariable String name){
        Classes student= studentService.getByName(name).get(0);
        return student;
    }

    //新增学生的方法，/webapi/student/insert
    @PostMapping("/insert")
    public R insert(@Validated Classes student, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String errorMsg=bindingResult.getFieldError().getDefaultMessage();
            log.info("新增课程出错了！{}",errorMsg);
            return RUtil.error(-10,errorMsg);
        }
        studentService.insert(student);
        /*Long myid= Long.valueOf(student.getId());
        return myid.intValue();*/

        return RUtil.success();
    }
//    public Student insert(Student student){
//
//        Student student1=studentService.insert(student);
//        return student1;
//    }

    //更新学生的方法，/webapi/student/update
    @PutMapping("/update")
    public R update(@Validated Classes student, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String errorMsg=bindingResult.getFieldError().getDefaultMessage();
            log.info("更新学生出错了！{}",errorMsg);
            return RUtil.error(-10,errorMsg);
        }
        studentService.update(student);
        /*Long myid= Long.valueOf(student.getId());
        return myid.intValue();*/

        return RUtil.success();
    }
//    public Student update(Student student){
//
//        Student student1=studentService.update(student);
//        return student1;
//    }


    //删除学生的方法，/webapi/student/delete
    @DeleteMapping ("/delete/{id}")
    public void delete(@PathVariable Long id){

        studentService.delete(id);

    }

}
